package com.baide.sanguanheyi.swing;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.springframework.stereotype.Component;

/**
 * @ClassName String2Voice
 * @Description TODO
 * @Author wangbing
 * @Date 2020/5/2 18:39
 **/
@Component
public class String2Voice {
    public void play(String voice) {
        ActiveXComponent ax = null;
        try {
            ax = new ActiveXComponent("Sapi.SpVoice");
            //运行时输出语音内容
            Dispatch spVoice = ax.getObject();
            // 音量 0-100
            ax.setProperty("Volume", new Variant(100));
            // 语音朗读速度 -10 到 +10
            ax.setProperty("Rate", new Variant(-1));
            // 执行朗读
            Dispatch.call(spVoice, "Speak", new Variant(voice));
            spVoice.safeRelease();
            ax.safeRelease();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
